package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.TVideo;
import org.rcloud.io.service.TVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: ink-feather
 * @Description:
 * @Date: 2019/6/3
 */
@RestController
@RequestMapping("/tvideo")
public class TVideoController {

    @Autowired
    private TVideoService tVideoService;

    @RequestMapping(value = "/insert", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo insert(@RequestBody JSONObject params){
        TVideo tVideo = JSON.toJavaObject(params, TVideo.class);
        return tVideoService.save(tVideo);
    }

    @RequestMapping(value = "/delete", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo delete(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return tVideoService.delete(id);
    }

    @RequestMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo update(@RequestBody JSONObject params){
        TVideo tVideo = JSON.toJavaObject(params, TVideo.class);
        return tVideoService.update(tVideo);
    }

    @RequestMapping(value = "/selectbyid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo selectById(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return tVideoService.selectById(id);
    }

    @RequestMapping(value = "/selectall", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo selectAll(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pageSize = (Integer) object.get("pageSize");
        return tVideoService.selectAll(pageNum,pageSize);
    }

    @RequestMapping(value = "/selectbylike", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo selectByLike(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pageSize = (Integer) object.get("pageSize");
        String vName = (String) object.get("vName");
        return tVideoService.selectByLike(pageNum,pageSize,vName);
    }
}
